package com.cloud.pole.rpc.collection;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.cloud.pole.api.user.UserCollectionApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.user.model.entity.collection.UserCollection;
import com.cloud.pole.user.service.AuthService;
import com.cloud.pole.user.service.UserService;
import com.cloud.pole.user.service.account.UserAccountDetailService;
import com.cloud.pole.user.service.collection.UserCollectionService;
import com.github.pagehelper.PageInfo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@RestController
@RequestMapping("api/userCollection")
public class UserCollectionRest {
	
	@Autowired
	private UserCollectionService userCollectionService;
	
    @RequestMapping(value = "/getUserCollection", method = RequestMethod.GET)
    public ApiRestResponse<UserCollectionApi> getUserCollection(Long userId, Integer objectTypeId, String objectId){   	
    	try {
			ParamException.err(StringUtils.isBlank(objectId), "objectId不能为空");
			UserCollection userCollection = userCollectionService.getUserCollection(userId, objectTypeId, objectId);
			UserCollectionApi userCollectionVo = null;
			if(userCollection != null) {
				userCollectionVo = new UserCollectionApi();
				BeanUtils.copyProperties(userCollection, userCollectionVo);
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userCollectionVo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "/addUserCollection", method = RequestMethod.GET)
    public ApiRestResponse<Boolean> addUserCollection(Long userId, String userName, String objectName, Integer objectTypeId, String objectId, String note){   	
    	try {
			ParamException.err(StringUtils.isBlank(objectId), "objectId不能为空");
			boolean flag = false;		
			userCollectionService.addCollection(userId, userName, objectId, objectName, objectTypeId, note);
			flag = true;
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, flag);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "/cancelUserCollection", method = RequestMethod.GET)
    public ApiRestResponse<Boolean> cancelUserCollection(Long userId, Integer objectTypeId, String objectId){	
    	try {
			ParamException.err(StringUtils.isBlank(objectId), "objectId不能为空");
			boolean flag = false;		
			userCollectionService.cancelUserCollection(userId, objectTypeId, objectId);
			flag = true;
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, flag);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
    
    @RequestMapping(value = "/getPageCollectionList", method = RequestMethod.GET)
    public ApiRestResponse<PageInfo<UserCollectionApi>> getPageCollectionList(Long userId, Integer objectTypeId, Integer pageNum, Integer pageSize){   	
    	try {
    		PageInfo pageInfo = userCollectionService.getPageCollectionList(userId, objectTypeId, pageNum, pageSize);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, pageInfo);
		} catch (BaseException e) {
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
    }
}
